<template>
  <div class="settings-view">
    <div class="settings-layout">
      <!-- 设置导航 -->
      <div class="settings-nav">
        <div
          v-for="item in navItems"
          :key="item.key"
          class="nav-item"
          :class="{ active: activeTab === item.key }"
          @click="activeTab = item.key"
        >
          {{ item.icon }} {{ item.label }}
        </div>
      </div>

      <!-- 设置内容 -->
      <div class="settings-content">
        <el-card>
          <!-- 基础设置 -->
          <div v-show="activeTab === 'basic'" class="setting-panel">
            <div class="panel-title">🔧 基础设置</div>
            <el-form :model="basicSettings" label-width="140px">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="最大并发数">
                    <el-input-number
                      v-model="basicSettings.maxConcurrency"
                      :min="1"
                      :max="10"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="请求延时 (秒)">
                    <el-input-number
                      v-model="basicSettings.requestDelay"
                      :min="0"
                      :max="10"
                      :step="0.1"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="超时时间 (秒)">
                    <el-input-number
                      v-model="basicSettings.timeout"
                      :min="5"
                      :max="300"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="重试次数">
                    <el-input-number
                      v-model="basicSettings.retryCount"
                      :min="0"
                      :max="10"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="浏览器设置">
                <el-checkbox v-model="basicSettings.headless">启用无头模式</el-checkbox>
                <el-checkbox v-model="basicSettings.saveLoginState">保存登录状态</el-checkbox>
                <el-checkbox v-model="basicSettings.autoRetry">自动重试失败任务</el-checkbox>
              </el-form-item>
            </el-form>
          </div>

          <!-- 网络设置 -->
          <div v-show="activeTab === 'network'" class="setting-panel">
            <div class="panel-title">🌐 网络设置</div>
            <el-form :model="networkSettings" label-width="140px">
              <el-form-item label="启用代理">
                <el-switch v-model="networkSettings.enableProxy" />
              </el-form-item>
              <template v-if="networkSettings.enableProxy">
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="代理地址">
                      <el-input v-model="networkSettings.proxyHost" placeholder="127.0.0.1" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="代理端口">
                      <el-input-number
                        v-model="networkSettings.proxyPort"
                        :min="1"
                        :max="65535"
                        style="width: 100%"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-form-item label="代理类型">
                      <el-select v-model="networkSettings.proxyType" style="width: 100%">
                        <el-option label="HTTP" value="http" />
                        <el-option label="HTTPS" value="https" />
                        <el-option label="SOCKS5" value="socks5" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="代理池数量">
                      <el-input-number
                        v-model="networkSettings.proxyPoolSize"
                        :min="1"
                        :max="20"
                        style="width: 100%"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </template>
              <el-form-item label="User-Agent">
                <el-input
                  v-model="networkSettings.userAgent"
                  type="textarea"
                  :rows="3"
                  placeholder="自定义User-Agent，留空使用默认值"
                />
              </el-form-item>
            </el-form>
          </div>

          <!-- 存储设置 -->
          <div v-show="activeTab === 'storage'" class="setting-panel">
            <div class="panel-title">💾 存储设置</div>
            <el-form :model="storageSettings" label-width="140px">
              <el-form-item label="默认存储方式">
                <el-radio-group v-model="storageSettings.defaultStorage">
                  <el-radio label="sqlite">SQLite数据库</el-radio>
                  <el-radio label="mysql">MySQL数据库</el-radio>
                  <el-radio label="json">JSON文件</el-radio>
                  <el-radio label="csv">CSV文件</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="数据存储路径">
                <el-input v-model="storageSettings.dataPath" placeholder="./data">
                  <template #append>
                    <el-button @click="selectDataPath">选择</el-button>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item label="自动备份">
                <el-switch v-model="storageSettings.autoBackup" />
              </el-form-item>
              <el-form-item v-if="storageSettings.autoBackup" label="备份间隔">
                <el-select v-model="storageSettings.backupInterval" style="width: 200px">
                  <el-option label="每小时" value="hourly" />
                  <el-option label="每天" value="daily" />
                  <el-option label="每周" value="weekly" />
                </el-select>
              </el-form-item>
              <el-form-item label="数据清理">
                <el-checkbox v-model="storageSettings.autoCleanup">自动清理过期数据</el-checkbox>
              </el-form-item>
              <el-form-item v-if="storageSettings.autoCleanup" label="保留天数">
                <el-input-number
                  v-model="storageSettings.retentionDays"
                  :min="1"
                  :max="365"
                  style="width: 200px"
                />
              </el-form-item>
            </el-form>
          </div>

          <!-- 通知设置 -->
          <div v-show="activeTab === 'notification'" class="setting-panel">
            <div class="panel-title">🔔 通知设置</div>
            <el-form :model="notificationSettings" label-width="140px">
              <el-form-item label="桌面通知">
                <el-switch v-model="notificationSettings.desktop" />
              </el-form-item>
              <el-form-item label="任务完成通知">
                <el-switch v-model="notificationSettings.taskComplete" />
              </el-form-item>
              <el-form-item label="错误通知">
                <el-switch v-model="notificationSettings.error" />
              </el-form-item>
              <el-form-item label="邮件通知">
                <el-switch v-model="notificationSettings.email" />
              </el-form-item>
              <template v-if="notificationSettings.email">
                <el-form-item label="邮箱地址">
                  <el-input v-model="notificationSettings.emailAddress" placeholder="your@email.com" />
                </el-form-item>
                <el-form-item label="SMTP服务器">
                  <el-input v-model="notificationSettings.smtpServer" placeholder="smtp.gmail.com" />
                </el-form-item>
              </template>
            </el-form>
          </div>

          <!-- 界面设置 -->
          <div v-show="activeTab === 'ui'" class="setting-panel">
            <div class="panel-title">🎨 界面设置</div>
            <el-form :model="uiSettings" label-width="140px">
              <el-form-item label="主题">
                <el-radio-group v-model="uiSettings.theme">
                  <el-radio label="light">浅色主题</el-radio>
                  <el-radio label="dark">深色主题</el-radio>
                  <el-radio label="auto">跟随系统</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="语言">
                <el-select v-model="uiSettings.language" style="width: 200px">
                  <el-option label="简体中文" value="zh-CN" />
                  <el-option label="English" value="en-US" />
                  <el-option label="日本語" value="ja-JP" />
                </el-select>
              </el-form-item>
              <el-form-item label="字体大小">
                <el-slider
                  v-model="uiSettings.fontSize"
                  :min="12"
                  :max="18"
                  :step="1"
                  show-stops
                  style="width: 200px"
                />
              </el-form-item>
              <el-form-item label="界面缩放">
                <el-slider
                  v-model="uiSettings.scale"
                  :min="80"
                  :max="150"
                  :step="10"
                  show-stops
                  style="width: 200px"
                />
              </el-form-item>
              <el-form-item label="启动时最小化">
                <el-switch v-model="uiSettings.startMinimized" />
              </el-form-item>
            </el-form>
          </div>

          <!-- 安全设置 -->
          <div v-show="activeTab === 'security'" class="setting-panel">
            <div class="panel-title">🔐 安全设置</div>
            <el-form :model="securitySettings" label-width="140px">
              <el-form-item label="数据加密">
                <el-switch v-model="securitySettings.encryption" />
              </el-form-item>
              <el-form-item label="访问密码">
                <el-switch v-model="securitySettings.requirePassword" />
              </el-form-item>
              <template v-if="securitySettings.requirePassword">
                <el-form-item label="设置密码">
                  <el-input
                    v-model="securitySettings.password"
                    type="password"
                    placeholder="请输入访问密码"
                    show-password
                  />
                </el-form-item>
              </template>
              <el-form-item label="自动锁定">
                <el-switch v-model="securitySettings.autoLock" />
              </el-form-item>
              <el-form-item v-if="securitySettings.autoLock" label="锁定时间 (分钟)">
                <el-input-number
                  v-model="securitySettings.lockTimeout"
                  :min="1"
                  :max="60"
                  style="width: 200px"
                />
              </el-form-item>
              <el-form-item label="日志记录">
                <el-checkbox v-model="securitySettings.logAccess">记录访问日志</el-checkbox>
                <el-checkbox v-model="securitySettings.logOperations">记录操作日志</el-checkbox>
              </el-form-item>
            </el-form>
          </div>

          <!-- 操作按钮 -->
          <div class="setting-actions">
            <el-button type="primary" @click="saveSettings">
              💾 保存设置
            </el-button>
            <el-button @click="resetSettings">
              🔄 重置默认
            </el-button>
            <el-button @click="exportSettings">
              📤 导出配置
            </el-button>
            <el-button @click="importSettings">
              📥 导入配置
            </el-button>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
const activeTab = ref('basic')

const navItems = [
  { key: 'basic', label: '基础设置', icon: '🔧' },
  { key: 'network', label: '网络设置', icon: '🌐' },
  { key: 'storage', label: '存储设置', icon: '💾' },
  { key: 'notification', label: '通知设置', icon: '🔔' },
  { key: 'ui', label: '界面设置', icon: '🎨' },
  { key: 'security', label: '安全设置', icon: '🔐' },
]

// 基础设置
const basicSettings = ref({
  maxConcurrency: 3,
  requestDelay: 2,
  timeout: 30,
  retryCount: 3,
  headless: false,
  saveLoginState: true,
  autoRetry: false,
})

// 网络设置
const networkSettings = ref({
  enableProxy: false,
  proxyHost: '127.0.0.1',
  proxyPort: 7890,
  proxyType: 'http',
  proxyPoolSize: 2,
  userAgent: '',
})

// 存储设置
const storageSettings = ref({
  defaultStorage: 'sqlite',
  dataPath: './data',
  autoBackup: true,
  backupInterval: 'daily',
  autoCleanup: false,
  retentionDays: 30,
})

// 通知设置
const notificationSettings = ref({
  desktop: true,
  taskComplete: true,
  error: true,
  email: false,
  emailAddress: '',
  smtpServer: '',
})

// 界面设置
const uiSettings = ref({
  theme: 'light',
  language: 'zh-CN',
  fontSize: 14,
  scale: 100,
  startMinimized: false,
})

// 安全设置
const securitySettings = ref({
  encryption: false,
  requirePassword: false,
  password: '',
  autoLock: false,
  lockTimeout: 15,
  logAccess: true,
  logOperations: true,
})

const selectDataPath = () => {
  ElMessage.info('选择数据存储路径')
  // 这里可以调用Tauri的文件选择API
}

const saveSettings = () => {
  ElMessage.success('设置已保存')
  // 这里可以保存设置到本地文件或数据库
}

const resetSettings = () => {
  ElMessageBox.confirm('确定要重置所有设置为默认值吗？', '确认重置', {
    type: 'warning',
  }).then(() => {
    ElMessage.success('设置已重置')
    // 这里可以重置所有设置
  })
}

const exportSettings = () => {
  ElMessage.success('配置已导出')
  // 这里可以导出配置文件
}

const importSettings = () => {
  ElMessage.info('选择配置文件')
  // 这里可以导入配置文件
}
</script>

<style scoped>
.settings-view {
  max-width: 1200px;
  margin: 0 auto;
}

.settings-layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 20px;
}

.settings-nav {
  background: white;
  border-radius: 8px;
  padding: 10px 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  height: fit-content;
}

.nav-item {
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.2s;
  border-left: 3px solid transparent;
  font-size: 14px;
}

.nav-item:hover {
  background: #f8f9fa;
}

.nav-item.active {
  background: var(--el-color-primary-light-9);
  border-left-color: var(--el-color-primary);
  color: var(--el-color-primary);
}

.settings-content {
  min-height: 600px;
}

.setting-panel {
  min-height: 500px;
}

.panel-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e4e7ed;
}

.setting-actions {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
  text-align: center;
}

.setting-actions .el-button {
  margin: 0 10px;
  padding: 10px 20px;
}

@media (max-width: 768px) {
  .settings-layout {
    grid-template-columns: 1fr;
  }
  
  .settings-nav {
    display: flex;
    overflow-x: auto;
    padding: 10px;
  }
  
  .nav-item {
    white-space: nowrap;
    border-left: none;
    border-bottom: 3px solid transparent;
  }
  
  .nav-item.active {
    border-left: none;
    border-bottom-color: var(--el-color-primary);
  }
}
</style>